<?php
include '../class/class.user.php';
include '../class/class.pegawai.php';
include '../class/class.kewenangan.php';
include '../class/class.golongan.php';

	if(isset($_REQUEST['aksi_rekam_user'])){
		$nip=$_POST['nip'];
		$password=$_POST['password'];
		$id_kewenangan=$_POST['id_kewenangan'];
		//$kdkanwil=$_POST['kdkanwil'];
		$msg=false;
		$error='';

		session_start();
		$id_parent_user=$_SESSION['id_user'];
		$level=$_SESSION['id_kewenangan'];

		$user= new User();

		$user->nip=$nip;
		$user->password=$password;
		$user->id_parent_user=$id_parent_user;
		$user->user_creating=$id_parent_user;
		$user->id_kewenangan=$id_kewenangan;
		//$user->kdkanwil=$kdkanwil;
        $user->status=0;
//		if($level==2){
//			$user->status=0;
//		}else{
//			$user->status=1;
//		}

		$userDao= new userDao();
		$cek=$userDao->cekUser($nip);
		if($cek){
			$error="User sudah ada";
		}else {
                    $pegawaiDao=new PegawaiDao();
                    $pegawai=$pegawaiDao->getPegawai($nip);
                    if($pegawai==false){
                        $error="NIP Tidak Terdaftar";
                    }else{
                        $data=$userDao->createUser($user);
                        if(!$data){
                                $error='Gagal Rekam User';
                        }else{
                                $msg=true;
                        }
                    }
		}

		$result=array(
			'msg' => $msg,
			'error' => $error,
			);

		echo json_encode($result);

	}

	if(isset($_REQUEST['aksi_hapus_user'])){
		$id_user=$_REQUEST['id_user'];

		$userDao= new UserDao();

		$user_del=$userDao->deleteUser($id_user);
		$msg=true;
		$error='';

		if($user_del){
			$msg=true;
		}else{
			$error="Gagal Hapus User";
		}

		$data=array(
			'msg' => $msg,
			'error' => $error,
			);

		echo json_encode($data);


	}

	if(isset($_REQUEST['aksi_aktivasi_user'])){
		$id_user=$_REQUEST['id_user'];

		$userDao= new UserDao();

		$user_act=$userDao->aktivasiUser($id_user);
		$msg=false;
		$error='';

		if($user_act){
			$msg=true;
		}else{
			$error="Gagal Aktivasi User";
		}

		$data=array(
			'msg' => $msg,
			'error' => $error,
			);

		echo json_encode($data);


	}
	if(isset($_REQUEST['aksi_deaktivasi_user'])){
		$id_user=$_REQUEST['id_user'];

		$userDao= new UserDao();

		$user_act=$userDao-deAktivasiUser($id_user);
		$msg=false;
		$error='';

		if($user_act){
			$msg=true;
		}else{
			$error="Gagal Aktivasi User";
		}

		$data=array(
			'msg' => $msg,
			'error' => $error,
			);

		echo json_encode($data);


	}

	if(isset($_REQUEST['aksi_get_all_kewenangan'])){
		session_start();

		$level=$_SESSION['id_kewenangan'];

		$kewenanganDao=new KewenanganDao();

		$kewenangan=$kewenanganDao->getAllKewenangan($level);

		echo json_encode($kewenangan);

	}

	if(isset($_REQUEST['aksi_get_user_ubah'])){
		$id_user=$_REQUEST['id_user'];

		$userdao=new UserDao();
		$user=new User();
		$user->id_user=$id_user;

		$user_ubah=$userdao->getUserById($user);

		$data=array(
			'id_user' => $user_ubah['id_user'],
			'nip' => $user_ubah['nip'],
			'id_kewenangan' => $user_ubah['id_kewenangan'],
			//'kdkanwil' => $user_ubah['kdkanwil'],
			);

		echo json_encode($data);
	}

	if(isset($_REQUEST['aksi_get_user_pic'])){
		$id_user=$_REQUEST['id_user'];

		$pegawaiDao= new PegawaiDao();
		$golonganDao= new GolonganDao();


		$userdao=new UserDao();
		$user=new User();
		$user->id_user=$id_user;

		$user_ubah=$userdao->getUserById($user);

		$pegawai=$pegawaiDao->getPegawai($user_ubah['nip']);
		$golongan= $golonganDao->getGolongan($pegawai['gol']);

		$data=array(
			'id_user' => $user_ubah['id_user'],
			'nip' => $user_ubah['nip'],
			'id_kewenangan' => $user_ubah['id_kewenangan'],
			'pegawai' => $pegawai,
			'gol' => $golongan,
			//'kdkanwil' => $user_ubah['kdkanwil'],
			);

		echo json_encode($data);
	}

	if(isset($_REQUEST['aksi_simpan_ubah_user'])){
		$id_user=$_POST['id_user'];
		$nip=$_POST['nip'];
		$password=$_POST['password'];
		$id_kewenangan=$_POST['id_kewenangan'];
		//$kdkanwil=$_POST['kdkanwil'];
		$msg=false;
		$error='Gagal Ubah User';

		session_start();
		$id_pengubah_user=$_SESSION['id_user'];

		$user= new User();
		$user->id_user=$id_user;
		$user->nip=$nip;
		$user->password=$password;
		//$user->kdkanwil=$kdkanwil;
		$user->id_parent=$id_pengubah_user;
		$user->id_kewenangan=$id_kewenangan;
		$userDao= new userDao();

		$ubah=$userDao->updateUser($user);

		if($ubah){
			$msg=true;
		}

		$data=array(
			'msg' => $msg,
			'error' => $error,
			);

		echo json_encode($data);
	}

	if(isset($_REQUEST['aksi_simpan_pic'])){
		$id=$_REQUEST['id'];
		$kewenangan=$_REQUEST['kewenangan'];
		$msg=false;

		session_start();
		$id_pengubah_user=$_SESSION['id_user'];

		$user= new User();
		$user->id_user=$id;

		$user->id_kewenangan=$kewenangan;
		$user->id_parent=$id_pengubah_user;
		$userDao= new userDao();

		$ubah=$userDao->updateUserPIC($user);

		if($ubah){
			$msg=true;
		}

		$data=array(
			'msg' => $msg,
			);

		echo json_encode($data);


	}

	if(isset($_REQUEST['aksi_get_user_ubah_pass'])){
		session_start();

		$id_user=$_SESSION['id_user'];

		$user=new User();
		$user->id_user=$id_user;

		$userDao=new UserDao();

		$user_data=$userDao->getUserById($user);

		$data=array(
			'nip' => $user_data['nip'],
			'id_user' => $id_user,
			);

		echo json_encode($data);
	}

	if(isset($_REQUEST['aksi_ubah_password'])){

		session_start();

		$id_user_pengubah=$_SESSION['id_user'];

		$id_user=$_POST['id_user'];
		$password_lama=md5($_POST['password_lama']);
		$password=$_POST['password'];
		$msg=false;
		$error='Gagal Ubah Password';

		$user=new User();
		$user->id_user=$id_user;

		$userDao= new UserDao();
		$user_data=$userDao->getUserById($user);

		if($password_lama==$user_data['password']){
			$user_ubah=new User();
			$user_ubah->id_user=$id_user;
			$user_ubah->password=$password;
			$user_ubah->id_parent=$id_user_pengubah;
			$user_ubah->nip=$user_data['nip'];

			$hasil=$userDao->updatePassword($user_ubah);
			if($hasil){
				$msg=true;
			}
		}else{
			$error='Password Lama Salah';
		}

		$data=array(
			'msg' => $msg,
			'error' => $error,
			);

		echo json_encode($data);

	}
?>